#include "form_device_notify.h"
#include "ui_form_device_notify.h"

// add nacessary includes here
#include "utils/can/can_bll_t1c.h"
#include "utils/configure/configure_bll.h"
#include "ui/share/form_shield_dlg.h"
#include "ui/share/form_prompt_dlg.h"

#define DF_CHANNEL_NUMBER       1

/*
 * Function:    FormDeviceNotify
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormDeviceNotify::FormDeviceNotify(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormDeviceNotify)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);
}

/*
 * FormHistoryResult:    ~FormDeviceNotify
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormDeviceNotify::~FormDeviceNotify()
{
    qDebug() << Q_FUNC_INFO;

    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormDeviceNotify" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormDeviceNotify" class
 */
FormDeviceNotify* FormDeviceNotify::getInstance(QWidget *pParent)
{
    static FormDeviceNotify *pNew = NULL;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormDeviceNotify(pParent);
        if(NULL == pNew) return NULL;

        // After constructing object, initialize the object
        pNew->_Initialze();

        // Initialize control's UI
        pNew->_InitUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    _Initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormDeviceNotify::_Initialze()
{
    qDebug() << Q_FUNC_INFO;

    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(this, SIGNAL(signalRefreshUI(bool)), this, SLOT(slotRefreshUI(bool)), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_InitUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormDeviceNotify::_InitUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/factory/device_notify/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->lineEdit_PCR->setPlaceholderText(tr("ms"));
    ui->lineEdit_PCR->setValidator(new QRegExpValidator(QRegExp("^[0-9]{0,6}$")));
    ui->lineEdit_PCR->setInputMethodHints(Qt::ImhDigitsOnly);
    ui->lineEdit_PCR->setText("500");

    ui->lineEdit_Heartbeat->setPlaceholderText(tr("ms"));
    ui->lineEdit_Heartbeat->setValidator(new QRegExpValidator(QRegExp("^[0-9]{0,6}$")));
    ui->lineEdit_Heartbeat->setInputMethodHints(Qt::ImhDigitsOnly);
    ui->lineEdit_Heartbeat->setText("30000");

    ui->checkBox_Sequential->setChecked(true);
    ui->checkBox_Temperature->setChecked(false);
    ui->checkBox_Fluorescence->setChecked(false);
    ui->checkBox_PCR->setChecked(false);
    ui->checkBox_Heartbeat->setChecked(false);

    ui->checkBox_Sequential->setFocusPolicy(Qt::NoFocus);
    ui->checkBox_Temperature->setFocusPolicy(Qt::NoFocus);
    ui->checkBox_Fluorescence->setFocusPolicy(Qt::NoFocus);
    ui->checkBox_PCR->setFocusPolicy(Qt::NoFocus);
    ui->checkBox_Heartbeat->setFocusPolicy(Qt::NoFocus);

    this->slotRefreshUI(true);
}

/*
 * Function:    on_pushButton_Save_clicked
 * Description: Save configuration info
 * Paramer:     void
 * Return:      void
 */
void FormDeviceNotify::on_pushButton_Save_clicked()
{
    qDebug() << Q_FUNC_INFO;

    // Prevent the button from clicking repeatedly quickly
    FormShieldDlg::getInstance(this)->show();

    // Save the detect params
    if(1 == 1) {
        ST_CFG_DETECT_PARAMS stCfgDetectParams;
        stCfgDetectParams.bPcrOvertop = ui->checkBox_ReactionOvertop->isChecked();
        stCfgDetectParams.bTipsExist = ui->checkBox_TipsExist->isChecked();
        stCfgDetectParams.bDrivepipeExist = ui->checkBox_DrivepipeExist->isChecked();
        stCfgDetectParams.bDrivepipeDislocation = ui->checkBox_DrivepipeDislocation->isChecked();

        ConfigureBLL::getInstance(this)->saveDetectParams(stCfgDetectParams);
    }

    // Save the notify params
    if(2 == 2) {
        QList<bool> lstCheckStatus = QList<bool>() << ui->checkBox_Sequential->isChecked()
                                                   << ui->checkBox_Temperature->isChecked()
                                                   << ui->checkBox_Fluorescence->isChecked()
                                                   << ui->checkBox_PCR->isChecked()
                                                   << ui->checkBox_Heartbeat->isChecked();

        int nPcrVal = ui->lineEdit_PCR->text().toInt();
        int nBeatVal = ui->lineEdit_Heartbeat->text().toInt();

        // Save configuration info
        int nRetVal = CanBLL_T1C::getInstance(this)->configNotifyEx(lstCheckStatus, nPcrVal, nBeatVal);
        if(0 == nRetVal) {
            ST_CFG_NOFIFY_PARAMS stCfgNotifyParams;
            stCfgNotifyParams.bSequential = ui->checkBox_Sequential->isChecked();
            stCfgNotifyParams.bTemperature = ui->checkBox_Temperature->isChecked();
            stCfgNotifyParams.bFluorescence = ui->checkBox_Fluorescence->isChecked();
            stCfgNotifyParams.bPCR = ui->checkBox_PCR->isChecked();
            stCfgNotifyParams.sPcrVal = ui->lineEdit_PCR->text().trimmed();
            stCfgNotifyParams.bHeartbeat = ui->checkBox_Heartbeat->isChecked();
            stCfgNotifyParams.sHeartbeatVal = ui->lineEdit_Heartbeat->text().trimmed();

            // Save the nofity params
            ConfigureBLL::getInstance(this)->saveNotifyParams(stCfgNotifyParams, DF_CHANNEL_NUMBER);

            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("Save successfully"));
            QTimer::singleShot(DEF_SEC_INTERVAL, this, [&]() {
                FormPromptWidget::getInstance(this)->hide();
            });
        }
        else {
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("Save failed") + QString(" [%1]").arg(nRetVal));
        }
    }

    FormShieldDlg::getInstance(this)->hide();
}

/*
* Function:		slotRefreshUI
* Description:	The slot that refresh UI info
* Paramer:		bool bInit - is first load
* Return:		void
*/
void FormDeviceNotify::slotRefreshUI(bool bInit)
{
    qDebug() << Q_FUNC_INFO << bInit;

    // Load the detect params
    if(1 == 1) {
        ST_CFG_DETECT_PARAMS stCfgDetectParams;
        ConfigureBLL::getInstance(this)->loadDetectParams(stCfgDetectParams);

        ui->checkBox_ReactionOvertop->setChecked(stCfgDetectParams.bPcrOvertop);
        ui->checkBox_TipsExist->setChecked(stCfgDetectParams.bTipsExist);
        ui->checkBox_DrivepipeExist->setChecked(stCfgDetectParams.bDrivepipeExist);
        ui->checkBox_DrivepipeDislocation->setChecked(stCfgDetectParams.bDrivepipeDislocation);
    }

    // Load the nofity params
    if(2 == 2) {
        ST_CFG_NOFIFY_PARAMS stCfgNotifyParams;
        ConfigureBLL::getInstance(this)->loadNotifyParams(stCfgNotifyParams, DF_CHANNEL_NUMBER);

        ui->checkBox_Sequential->setChecked(stCfgNotifyParams.bSequential);
        ui->checkBox_Temperature->setChecked(stCfgNotifyParams.bTemperature);
        ui->checkBox_Fluorescence->setChecked(stCfgNotifyParams.bFluorescence);
        ui->checkBox_PCR->setChecked(stCfgNotifyParams.bPCR);
        ui->checkBox_Heartbeat->setChecked(stCfgNotifyParams.bHeartbeat);
        ui->lineEdit_PCR->setText(stCfgNotifyParams.sPcrVal);
        ui->lineEdit_Heartbeat->setText(stCfgNotifyParams.sHeartbeatVal);

#ifdef QT_NO_DEBUG
        if(bInit) {
            QList<bool> lstCheckStatus = QList<bool>() << stCfgNotifyParams.bSequential
                                                       << stCfgNotifyParams.bTemperature
                                                       << stCfgNotifyParams.bFluorescence
                                                       << stCfgNotifyParams.bPCR
                                                       << stCfgNotifyParams.bHeartbeat;

            int nPcrVal = ui->lineEdit_PCR->text().toInt();
            int nBeatVal = ui->lineEdit_Heartbeat->text().toInt();

            // Save configuration info
            int nRetVal = CanBLL_T1C::getInstance(this)->configNotifyEx(lstCheckStatus, nPcrVal, nBeatVal);
            if(0 == nRetVal) {
                qDebug() << Q_FUNC_INFO << "Notify configure successfully";
            }
            else {
                qDebug() << Q_FUNC_INFO << "Notify configure failed" + QString(" [%1]").arg(nRetVal);
            }
        }
#endif
    }

    if(3 == 3) {
        ui->tabWidget->setCurrentIndex(0);
        ui->tabWidget->tabBar()->setHidden(true);

        // Get the login mode
        ST_CFG_LOGIN_MODE stLoginMode;
        ConfigureBLL::getInstance(this)->loadLoginMode(stLoginMode);

        switch ((int)stLoginMode.eumLoginMode) {
        case E_MODE_FACTORY:
            ui->tabWidget->tabBar()->setHidden(false);
            break;
        default: break;
        }
    }

    ui->tabWidget->setCurrentIndex(0);
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void FormDeviceNotify::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
    {
        ui->retranslateUi(this);

        ui->lineEdit_PCR->setPlaceholderText(tr("ms"));
        ui->lineEdit_Heartbeat->setPlaceholderText(tr("ms"));

        qDebug() << Q_FUNC_INFO;
        break;
    }
    default:
        break;
    }

    QWidget::changeEvent(e);
}
